import { BaseEntity } from '@/modules/core/base/BaseEntity';
import {
  Column,
  DeleteDateColumn,
  Entity,
  JoinColumn,
  JoinTable,
  ManyToMany,
  OneToMany,
  OneToOne,
} from 'typeorm';
import { CartsEntity } from './cart.entity';
import { GoodCategoryEntity } from './category.entity';
import { OrderGoodEntity } from './order_good.entity';
import { MediaEntiry } from '@/modules/media/entities';
import { Type } from 'class-transformer';

@Entity('good')
export class GoodEntity extends BaseEntity {
  @Column({
    comment: '商品名称',
  })
  name: string;

  @Column({ comment: '商品描述' })
  description: string;

  @Column({ comment: '商品价格' })
  price: number;

  @Column({ comment: '商品原始价格' })
  oldPrice?: number;

  @Column({ comment: '商品连接', default: '' })
  link?: string;

  @Column({ comment: '关键词', type: 'simple-array', nullable: true })
  keywords?: string[];

  @OneToMany((type) => CartsEntity, (cart) => cart.good, {
    onDelete: 'CASCADE',
  })
  carts: CartsEntity[];

  @OneToMany((type) => OrderGoodEntity, (order_good) => order_good.good, {
    onDelete: 'CASCADE',
  })
  orderGood: OrderGoodEntity[];

  @ManyToMany(() => GoodCategoryEntity, {
    cascade: true,
  })
  @JoinTable()
  categories!: GoodCategoryEntity[];

  @OneToOne((type) => MediaEntiry, (media) => media.good, {
    onDelete: 'CASCADE',
  })
  @JoinColumn()
  media: MediaEntiry;

  @OneToOne((type) => MediaEntiry, (media) => media.goodIcon, {
    onDelete: 'CASCADE',
  })
  @JoinColumn()
  mediaIcon: MediaEntiry;

  @DeleteDateColumn({
    comment: '删除时间',
  })
  @Type(() => Date)
  deleteAt: Date;
}
